package com.learn.activiti.config;

import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.*;
import org.activiti.spring.SpringAsyncExecutor;
import org.activiti.spring.SpringProcessEngineConfiguration;
import org.activiti.spring.boot.AbstractProcessEngineAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;
import java.io.IOException;

/**
 * <一句话描述>
 *
 * @author xianglujun
 * @date 2019/7/10 15:09
 */
@Configuration
@Slf4j
public class AutoConfiguration extends AbstractProcessEngineAutoConfiguration {

    @Bean
    public SpringProcessEngineConfiguration getProcessEngineConfiguration(DataSource dataSource,
                                                                          PlatformTransactionManager transactionManager,
                                                                          SpringAsyncExecutor asyncExecutor) throws IOException {
        SpringProcessEngineConfiguration processEngineConfiguration =
                baseSpringProcessEngineConfiguration(dataSource, transactionManager, asyncExecutor);

        // 设置加载模式, 用于在流程文件发生变更是, 进行变更验证
        //  default: 包所有资源发在一个单独的包中，对这个包进行重复发布监测
        //  single-resource: 为每个单独的资源创建一个发布包, 并对这些发布包进行重复性监测.
        //  resource-parent-folder: 把在同一个目录下的文件放在一个发布包中, 对发布包进行重复性监测
        processEngineConfiguration.setDeploymentMode("single-resource");
        // 部署的时候创建图片
        processEngineConfiguration.setCreateDiagramOnDeploy(true);
        return processEngineConfiguration;
    }

//    @Bean
//    public ProcessEngine getProcessEngine(SpringProcessEngineConfiguration processEngineConfiguration) throws Exception {
//        ProcessEngineFactoryBean processEngineFactory = new ProcessEngineFactoryBean();
//        processEngineFactory.setProcessEngineConfiguration(processEngineConfiguration);
//        return processEngineFactory.getObject();
//    }

    @Bean
    public TaskService getTaskService(ProcessEngine processEngine) {
        return processEngine.getTaskService();
    }

    @Bean
    public FormService getFormService(ProcessEngine processEngine) {
        return processEngine.getFormService();
    }

    @Bean
    public HistoryService getHistoryService(ProcessEngine processEngine) {
        return processEngine.getHistoryService();
    }

    @Bean
    public IdentityService getIdentityService(ProcessEngine processEngine) {
        return processEngine.getIdentityService();
    }

    /**
     * 它提供了管理和控制发布包和流程定义的操作。
     * @param processEngine
     * @return
     */
    @Bean
    public RepositoryService getRepositoryService(ProcessEngine processEngine) {
        return processEngine.getRepositoryService();
    }

    /**
     * 用来保存流程实例在执行过程中创建的变量等信息, 这些数据是用于某一个特定的实例. 并会被流程中的很多实例所使用
     * @param processEngine
     * @return
     */
    @Bean
    public RuntimeService getRuntimeService(ProcessEngine processEngine) {
        return processEngine.getRuntimeService();
    }

}
